package com.cfets.annualAssessment.Servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by ShenYiChou on 2017/7/25.
 */
@WebServlet(urlPatterns = { "/admin/CheckVerifyCode/restful" })
public class CheckCodeServlet extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        ServletOutputStream out = response.getOutputStream();

        HttpSession session = request.getSession(false);
        if(session == null) {
            out.write("验证码处理问题".getBytes("UTF-8"));
            out.flush();
            out.close();
            return;
        }

        String saveCode = (String)session.getAttribute("validate_code");
        if(saveCode == null) {
            out.write("验证码处理问题".getBytes("UTF-8"));
            out.flush();
            out.close();
            return;
        }

        String checkCode = request.getParameter("validate_code");
        if(!saveCode.equals(checkCode)) {
            out.write("验证码无效!".getBytes("UTF-8"));
            out.flush();
            out.close();
            return;
        }

        session.removeAttribute("validate_code");
        out.write("true".getBytes("UTF-8"));
        out.flush();
        out.close();

    }
}
